﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class UCOrderStatus : UserControl
    {
        private static MySqlConnection DBCon;

        public UCOrderStatus()
        {
            InitializeComponent();            
            this.OrderStatusDataGrid.CellContentDoubleClick+=new DataGridViewCellEventHandler(OrderStatusDataGrid_CellContentDoubleClick);
            OrderStatusDataGrid.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            DBCon = new MySqlConnection(Program.getConnection());
            qSalesOrder();
        }

        public void qSalesOrder()
        {
            string query = "";
            if (Employee.Permission == 1)
            {
                query = "SELECT u.name AS name, u.Balance, s.unit_id, s.order_id, s.timestamp, s.status AS status, s.ApprovedBy FROM UNIT AS u, S_ORDER AS s WHERE u.unit_id = s.unit_id AND s.status = 1 ORDER by timestamp DESC";

            }
            else if (Employee.Permission == 2)
            {
                query = "SELECT u.name AS name, u.Balance, s.unit_id, s.order_id, s.timestamp, s.status AS status, s.ApprovedBy FROM UNIT AS u, S_ORDER AS s WHERE u.unit_id = s.unit_id AND s.status != 3 ORDER by timestamp DESC";
            }
            try
            {
                OrderStatusDataGrid.DataSource = Query.select(query);

                for (int i = 0; i < OrderStatusDataGrid.Rows.Count; i++)
                {
                    OrderStatusDataGrid.Rows[i].Cells["status"].Value = statusName(int.Parse((string)OrderStatusDataGrid.Rows[i].Cells["status"].Value));
                }
            }
            catch (MySqlException exc) { MessageBox.Show(exc.Message); }
        }

        public static string statusName(int num)
        {
            string name = "";
            switch (num)
            {
                case 0:
                    name = "Pending";
                    break;
                case 1:
                    name = "Active";
                    break;
                case 2:
                    name = "Ready to Ship";
                    break;
                case 3:
                    name = "Shipped";
                    break;
            }
            return name;
        }


        private void OrderStatusDataGrid_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            FormOrderSummary exists = Application.OpenForms.OfType<FormOrderSummary>().FirstOrDefault();
            if (exists == null)
            {
                FormOrderSummary fos = new FormOrderSummary((int)OrderStatusDataGrid.Rows[e.RowIndex].Cells["order_id"].Value, OrderStatusDataGrid.Rows[e.RowIndex].Cells["name"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["Status"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["timestamp"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["ApprovedBy"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["Balance"].Value.ToString());
                fos.StartPosition = FormStartPosition.CenterScreen;
                fos.Show();
            }
            else
            {
                exists.Close();
                exists.Dispose();
                FormOrderSummary fos = new FormOrderSummary((int)OrderStatusDataGrid.Rows[e.RowIndex].Cells["order_id"].Value, OrderStatusDataGrid.Rows[e.RowIndex].Cells["name"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["Status"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["timestamp"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["ApprovedBy"].Value.ToString(), OrderStatusDataGrid.Rows[e.RowIndex].Cells["Balance"].Value.ToString());
                fos.StartPosition = FormStartPosition.CenterScreen;
                fos.Show();
            }
        }
    }
}
